package org.example.mytest.control;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.example.mytest.entity.User;
import org.example.mytest.mapper.UserMapper;
import org.example.mytest.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //    -------------------------------查询--------------------------
    @GetMapping("/user/query1")
    public String query1(){
        List<User> list=userMapper.selectList(null);
        System.out.println(list);
        return "查询用户";
    }
    @GetMapping("/user/query2")
    public List query2() {
        List<User> list = userMapper.selectList(null);
        return list;
    }
    @GetMapping("/user/query3") //按照id查询
    public User query3(){
        int id=1;
        User user = userMapper.selectById(id);
        System.out.println(user);
        return user;
    }
    @GetMapping("/user/query4") //按照id查询单个
    public User query44(int id){
        User user = userMapper.selectById(id);
        System.out.println(user);
        return user;
    }
    @GetMapping("/user/query5") //按照id查询多个
    public List<User> query5(){
        List<User> list=userMapper.selectBatchIds(Arrays.asList(1,2,3));
        return list;
    }
    @GetMapping("/user/query6") //selectByMap
    public List<User> query6() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","Cory");
        List<User> list=userMapper.selectByMap(map);
        System.out.println(list);
        return list;
    }
    //    eq、ne、gt、ge、lt、le：
//这些方法分别表示等于、不等于、大于、大于等于、小于、小于等于的条件。
    @GetMapping("/user/query7")
    public List<User> query7(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("age",19);
        List<User> list=userMapper.selectList(queryWrapper);
        System.out.println(list);
        return list;
    }
    //like、notLike、likeLeft、likeRight：用于构建模糊查询条件。
    @GetMapping("/user/query8")
    public List<User> query8(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","n");
        queryWrapper.orderByAsc("age");
        List<User> list=userMapper.selectList(queryWrapper);
        System.out.println(list);
        return list;
    }
    //    -------------------------------插入--------------------------
    @PostMapping("/user/insert1") //需要借助Swagger或apiPost模拟提交数据
    public String insert1(User user){
        int i=userMapper.insert(user);
        if(i>0){
            return "插入成功";
        }
        else {
            return "插入失败";
        }
    }
    @PostMapping("/user/insert2") //
    public String insert2(){
        User user=new User();
        user.setId(20);
        user.setName("ff");
        user.setAge(15);
        user.setEmail("ff@163.com.cn");
        int i=userMapper.insert(user);
        if(i>0){
            return "插入成功";
        }
        else {
            return "插入失败";
        }
    }
    @PostMapping("/user/insert3")
    public String insert3()
    {
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("name","Cory");
        updateWrapper.set("name","Corynew");
        int result = userMapper.update(null, updateWrapper);

        if (result > 0) {
            return "修改成功";
        } else {
            return "修改失败";

    }
}
}
